<?php

    class CreditCard {

        private $FullName;
        private $CardNo;
        private $Type;
        private $ExpirationDate;

        public function __construct($fullname, $cardno, $type, $expiration) {
            $this->FullName = $fullname;
            $this->CardNo = $cardno;
            $this->Type = $type;
            $this->ExpirationDate = $expiration;
        }

        public function __get($key) {
            if(property_exists($this, $key)) {
                return $this->{$key};
            }
            else {
                return NULL;
            }
        }

        public function __set($key, $value) {
            throw new Exception('Class is immutable.');
        }

        public static function InsertCreditCard($creditcard) {
            Connection::Connect();
            if(($try = CreditCard::ReadCreditCard($creditcard->CardNo)) != NULL)
                return $try->CardNo;

            $nameid = FullName::InsertFullName($creditcard->FullName);

            $sql = mysql_query(
                "
                    INSERT INTO 
                    CreditCard (
                        NameId, 
                        CardNo, 
                        Type, 
                        ExpirationDate
                    ) 
                    VALUES (
                        '$nameid', 
                        '$creditcard->CardNo', 
                        '$creditcard->Type', 
                        '".$creditcard->ExpirationDate->format('Y-m-d')."'
                    )
                "
            );

            if($sql == FALSE)
                return NULL;

            return $creditcard->CardNo;
        }

        public static function ReadCreditCard($creditcardno) {
            Connection::Connect();
            $sql = mysql_query(
                "
                    SELECT 
                        *
                    FROM 
                        CreditCard 
                    WHERE
                        CardNo = '$creditcardno'
                "
            );

            if($sql == NULL)
                return NULL;;

            $rows = mysql_num_rows($sql);
            if($rows == 1) {
                while($row = mysql_fetch_assoc($sql))
                {
                    $nameid = $row['NameId'];
                    $cardno = $row['CardNo'];
                    $type = $row['Type'];
                    $expiration = DateTime::createFromFormat("Y-m-d", $row['ExpirationDate']);
                    return new CreditCard(FullName::ReadFullName($nameid), $cardno, $type, $expiration);
                }
            }
            return NULL;
        }

        public static function UpdateCreditCard($creditcard) {
            Connection::Connect();
            $sql = mysql_query(
                "
                    UPDATE 
                        CreditCard 
                    SET 
                        NameId = '".FullName::UpdateFullName($creditcard->FullName)."', 
                        Type = '$creditcard->Type', 
                        ExpirationDate = '".$creditcard->ExpirationDate->format('Y-m-d')."' 
                    WHERE 
                        CardNo = '$creditcard->CardNo'
                " 
            );

            if($sql == FALSE)
                return NULL;

            return $creditcard->CardNo;
        }

    }

?>